/*
 * @Descripttion: 胡腾辉 | feico
 * @version: V1.0
 * @Author: feico
 * @Date: 2022-02-22 16:18:33
 * @LastEditors: feico
 * @LastEditTime: 2022-02-23 16:46:49
 */
import request from '../../utils/request'
import { GET_CHANNEL_LIST,GET_ARTICLE_LIST } from '../constants'

export const getChannelAc=(data)=>({
type:GET_CHANNEL_LIST,
data
})

export const getArticleListAc=(data)=>({
    type:GET_ARTICLE_LIST,
    data
})

// 请求频道列表
export const getChannel = ()=>{
    return async(dispatch)=>{
        const res = await request.get('/channels')
        dispatch(getChannelAc(res.data.data.channels))
    }
}

// 请求文章列表
export const getArticleList=(params)=>{
    return async(dispatch)=>{
        const res = await request({
            url:'/mp/articles',
            params
        })
        dispatch(getArticleListAc(res.data.data))
    }
}

// 删除文章
export const delArticlebyId = (id) => {
    return async()=>{
        await request({
            method:'delete',
            url:`/mp/articles/${id}`
        })
    }
}

// 添加文章
export const addArticle = (data,draft = false) => {
    return async () => {
        await request({
            url: '/mp/articles',
            method: 'post',
            data,
            params: {
                draft,
            },
        })
    }
}

// 根据id数据回显
export const getArticleById =(id)=>{
    return async()=>{
       const res = await request(`/mp/articles/${id}`)
       return res.data.data
    }
}

// 编辑完成
export const editArticle=(data,draft = false)=>{
    return async()=>{
        await request({
            method:'put',
            url:`/mp/articles/${data.id}`,
            data,
            params:{
                draft
            }
        })
    }
}